from django.shortcuts import render,render_to_response,HttpResponse
from blog import models
from blog.extensions import Html,global_Tools
# from blog.extensions import global_Tools
# Create your views here.
def index(request):
	# models.News.objects.all()
	# all_data = models.News.objects.filter(id__gt=0)[0:5]
	all_data = models.News.objects.filter(id__gt=0)
	return render_to_response('index.html',{'result':all_data})



def index_page(request,page):
	page = global_Tools.try_int(page,1)
	all_data = models.News.objects.filter(id__gt=0).count()  
	pageInfoObj = Html.PageInfo(page,all_data,peritems=5)
	pageNum = pageInfoObj.currentPage()
	# print all_data
	# print pageNum[0]
	# print pageNum[1]
	list_page = range(1,pageNum[0]+1)
	all_data = models.News.objects.filter(id__gt=0)[pageInfoObj.From():pageInfoObj.To()]
	if page == 0: 
		upprev = page - 1
	else:
		upprev = 1

	if page != len(list_page):
		prev = page + 1
	else:
		prev = len(list_page) 


	return render_to_response('index.html',{'result':all_data,'page':list_page,'prev':prev,'upprev':upprev})
#,'page':range(1,pageNum[0])
#totalpage totalnum pageNum